package net.fpeg.kotlin.msa.wordbase.controller

import net.fpeg.kotlin.msa.wordbase.dto.WordUserRelationTypeSelectDto
import net.fpeg.kotlin.msa.wordbase.dto.WordUserRelationTypeSelectIndexDto
import net.fpeg.kotlin.msa.wordbase.service.WordUserRelationTypeService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*


@RestController
@RequestMapping("wordUserRelationType")
class WordUserRelationTypeController {
    @Autowired
    lateinit var wordUserRelationTypeService: WordUserRelationTypeService

    @GetMapping
    fun fetch(
            @RequestHeader("USER_ID") uid: Long
    ): WordUserRelationTypeSelectDto =
            WordUserRelationTypeSelectDto(
                    wordUserRelationTypeService.fetch(uid)
                            .map { d ->
                                d.run {
                                    WordUserRelationTypeSelectIndexDto(id ?: throw Exception("未知错误"), value)
                                }
                            })

    @PostMapping("/{value}")
    @ResponseStatus(HttpStatus.CREATED)
    fun add(
            @RequestHeader("USER_ID") id: Long,
            @PathVariable("value") value: String
    ) = wordUserRelationTypeService.add(id, value).id

    @PutMapping("/{id}/{value}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    fun edit(
            @RequestHeader("USER_ID") id: Long,
            @PathVariable("id") typeId: Long,
            @PathVariable("value") value: String
    ) {
        wordUserRelationTypeService.edit(id, typeId, value)
    }


    @DeleteMapping("/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    fun remove(
            @RequestHeader("USER_ID") id: Long,
            @PathVariable("id") typeId: Long
    ) {
        wordUserRelationTypeService.remove(id, typeId)
    }


}